package com.bizfx.component.views
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import com.bizfx.collections.HashMap;
	import com.bizfx.collections.IMap;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.containers.Box;
	import mx.containers.HDividedBox;
	import mx.containers.VBox;
	import mx.containers.VDividedBox;
	import mx.core.Container;
	import mx.events.FlexEvent;
	
	[Event(name="listViewSetted", type="flash.events.Event")]
	
	[Event(name="detailViewSetted", type="flash.events.Event")]
	
	public class BaseListDetailView extends BaseView implements IListDetailView
	{
		public static const LayoutType_Full:String = "full";
		
		public static const LayoutType_VerticalSeparate:String = "verticalseparate";
		
		public static const LayoutType_HorizontalSeparate:String = "horizontalseparate";
		
		private var fullContainer:VBox;
		
		private var verticalSeparater:VDividedBox;
		
		private var horizontalSeparater:HDividedBox;
		
		private var layouts:IList = new ArrayCollection();
		
		private var _listView:IListView;
		[Bindable]
		public function get listView():IListView
		{
			return _listView;
		}
		
		public function set listView(value:IListView):void {
			if (_listView != value) {
				_listView = value;
				if (_listView != null) {
					_listView.viewKey = "listView";
					this.dispatchEvent(new Event("listViewSetted", true));
					this.initLayout(this.layout);
				}
			}
		}
		
		private var _detailView:IDetailView;
		[Bindable]
		public function get detailView():IDetailView
		{
			return _detailView;
		}
		
		public function set detailView(value:IDetailView):void {
			if (_detailView !== value) {
				_detailView = value;
				if (_detailView != null) {
					_detailView.viewKey = "detailView";
					this.dispatchEvent(new Event("detailViewSetted", true));
					this.initLayout(this.layout);
				}
			}
		}
	
		private var _layoutType:String = LayoutType_Full;
		[Bindable]
		public function get layoutType():String {
			return _layoutType;
		}
		
		public function set layoutType(value:String):void {
			_layoutType = value;
		}
		
		private function get layout():Container {
			switch (this.layoutType) {
				case LayoutType_Full:
					if (!this.fullContainer) {
						fullContainer = new VBox();
						fullContainer.percentHeight = 100;
						fullContainer.percentWidth = 100;
						layouts.addItem(this.fullContainer);
					}
					return this.fullContainer;
					break;
				case LayoutType_VerticalSeparate:
					if (!this.horizontalSeparater) {
						verticalSeparater = new VDividedBox();
						verticalSeparater.percentHeight = 100;
						verticalSeparater.percentWidth = 100;
						layouts.addItem(this.verticalSeparater);
					}
					return this.verticalSeparater;
					break;
				case LayoutType_HorizontalSeparate:
					if (!this.horizontalSeparater) {
						horizontalSeparater = new HDividedBox();
						horizontalSeparater.percentHeight = 100;
						horizontalSeparater.percentWidth = 100;
						layouts.addItem(this.horizontalSeparater);
					}
					return this.horizontalSeparater;
					break;
				default:
					if (!this.fullContainer) {
						fullContainer = new VBox();
						fullContainer.percentHeight = 100;
						fullContainer.percentWidth = 100;
						layouts.addItem(this.fullContainer);
					}
					return this.fullContainer;
					break;
			}
		}
		
		public function BaseListDetailView():void {
			super();
			this.percentHeight = 100;
			this.percentWidth = 100;
		}
		
		override protected function createChildren():void {
			super.createChildren();
			this.initLayout(this.layout);
		}
		
		private function initLayout(container:Container):void {
			for each(var layout:DisplayObject in (this.layouts as DisplayObject)) {
				if (this.contains(layout)) {
					this.removeChild(layout);
				}
			}
			if (this.listView != null) {
				if (!container.contains(this.listView as DisplayObject)) {
					container.addChild(this.listView as DisplayObject);
				}
				this.listView.percentHeight = 100;
				this.listView.percentWidth = 100;
				this.listView.show();
			}
			
			if (this.detailView != null) {
				if (!container.contains(this.detailView as DisplayObject)) {
					container.addChild(this.detailView as DisplayObject);
				}
				this.detailView.percentHeight = 100;
				this.detailView.percentWidth = 100;
				this.detailView.hide();
			}
			if (!this.contains(container)) {
				this.addChild(container);
			}
		}
		
		private var listDetailCommandMap:IMap = new HashMap();
		
		public function addListDetailCommandMap(listCommandKey:String, detailCommandKey:String):void {
			var detailCommands:IList;
			if (listDetailCommandMap.containsKey(listCommandKey)) {
				detailCommands = listDetailCommandMap.get(listCommandKey) as IList;
				
				if (detailCommands.getItemIndex(detailCommandKey) < 0) {
					detailCommands.addItem(detailCommandKey);
					this.listDetailCommandMap.put(listCommandKey, detailCommands);
				}
			} else {
				detailCommands = new ArrayCollection();
				detailCommands.addItem(detailCommandKey);
				this.listDetailCommandMap.put(listCommandKey, detailCommands);
			}
		}
		
		public function getListDetailCommands(listCommandKey:String):IList {
			var result:IList;
			if (this.listDetailCommandMap.containsKey(listCommandKey)) {
				return this.listDetailCommandMap.get(listCommandKey) as IList;
			} else {
				return new ArrayCollection();
			}
		}
	}
}